<?php
require_once __DIR__."/Base_model.php";
class Host_music_model extends Base_model
{
    public function __construct()
    {
        parent::__construct();
        $this->music_type="music_type";//音效分类表
        $this->music="music";//音效表
    }

    /**
     * 音效主页
     */
    public function all($arr){
        $this->db->select('*');
        $this->db->from($this->music_type);
        //分页
        if(!empty($arr['page']) && !empty($arr['limit'])){
            $pages=($arr['page']-1)*$arr['limit'];
            $datas['total']=$this->db->count_all_results("",false);
            $this->db->limit($arr['limit'],$pages);
        }else{
            $datas['total']=$this->db->count_all_results("",false);
        }
        //排序
        $this->db->order_by('music_tmp',"desc");
        $res = $this->db->get();
        $datas['row']= $res->result_array();
//        echo $this->db->last_query();
        return $datas;
    }

    /**
     * 查询每个音效下面的音效个数
     */
    public function all_nums($id){
        $this->db->select('id');
        $this->db->from($this->music);
        $this->db->where("music_type_id",$id);
        $res = $this->db->get();
        $datas= $res->result_array();
        return $datas;
    }

    /**
     * 查询这个音效下的所有音效
     */
    public function music_all($id){
        $this->db->select('*');
        $this->db->from($this->music);
        $this->db->where("music_type_id",$id);
        $this->db->order_by('music_time_tmp',"desc");
        $res = $this->db->get();
        $datas= $res->result_array();
        return $datas;
    }

    /**
     * 音效分类上移
     */
    public function up_type($start_id,$now_id){
        //1.当前的排序位置
        $now=$this->one($this->music_type,['id'=>$now_id],"id,music_tmp");
        //2.这个排序值上一个的排序位置
        $start=$this->one($this->music_type,['id'=>$start_id],"id,music_tmp");

        $this->db->trans_begin();// 开启事务
        //3.先把现在的排序位置改为以前的
        $this->db->update($this->music_type, ['music_tmp' => $start['music_tmp']],['id'=>$now['id']]);
        $arr = $this->db->affected_rows();//受影响的行数
        //4.再把以前的排序位置改为现在的
        $this->db->update($this->music_type, ['music_tmp' => $now['music_tmp']],['id'=>$start['id']]);
        if ($this->db->trans_status() === false || $arr == 0) {
            $this->db->trans_rollback();//回滚
            return false;
        } else {
            $this->db->trans_commit();//提交事物
            return true;
        }
    }

    /**
     * 音效分类下移
     */
    public function down_type($end_id,$now_id){
        //1.当前的排序位置
        $now=$this->one($this->music_type,['id'=>$now_id],"id,music_tmp");
        //2.这个排序值上一个的排序位置
        $end=$this->one($this->music_type,['id'=>$end_id],"id,music_tmp");

        $this->db->trans_begin();// 开启事务
        //3.先把现在的排序位置改为以前的
        $this->db->update($this->music_type, ['music_tmp' => $end['music_tmp']],['id'=>$now['id']]);
        $arr = $this->db->affected_rows();//受影响的行数
        //4.再把以前的排序位置改为现在的
        $this->db->update($this->music_type, ['music_tmp' => $now['music_tmp']],['id'=>$end['id']]);
        if ($this->db->trans_status() === false || $arr == 0) {
            $this->db->trans_rollback();//回滚
            return false;
        } else {
            $this->db->trans_commit();//提交事物
            return true;
        }
    }

    /**
     * 音效上移
     */
    public function up_music($now_id,$start_id){
        //1.当前的排序位置
        $now=$this->one($this->music,['id'=>$now_id],"id,music_time_tmp");
        //2.这个排序值上一个的排序位置
        $start=$this->one($this->music,['id'=>$start_id],"id,music_time_tmp");
        $this->db->trans_begin();// 开启事务
        //3.先把现在的排序位置改为以前的
        $this->db->update($this->music, ['music_time_tmp' => $start['music_time_tmp']],['id'=>$now['id']]);
        $arr = $this->db->affected_rows();//受影响的行数
        //4.再把以前的排序位置改为现在的
        $this->db->update($this->music, ['music_time_tmp' => $now['music_time_tmp']],['id'=>$start['id']]);
        if ($this->db->trans_status() === false || $arr == 0) {
            $this->db->trans_rollback();//回滚
            return false;
        } else {
            $this->db->trans_commit();//提交事物
            return true;
        }
    }

    /**
     * 音效下移
     */
    public function down_music($now_id,$end_id){
        //1.当前的排序位置
        $now=$this->one($this->music,['id'=>$now_id],"id,music_time_tmp");
        //2.这个排序值上一个的排序位置
        $end=$this->one($this->music,['id'=>$end_id],"id,music_time_tmp");

        $this->db->trans_begin();// 开启事务
        //3.先把现在的排序位置改为以前的
        $this->db->update($this->music, ['music_time_tmp' => $end['music_time_tmp']],['id'=>$now['id']]);
        $arr = $this->db->affected_rows();//受影响的行数
        //4.再把以前的排序位置改为现在的
        $this->db->update($this->music, ['music_time_tmp' => $now['music_time_tmp']],['id'=>$end['id']]);
        if ($this->db->trans_status() === false || $arr == 0) {
            $this->db->trans_rollback();//回滚
            return false;
        } else {
            $this->db->trans_commit();//提交事物
            return true;
        }
    }

    /**
     * 发布
     */
    public function fabu(){
        //1.先去查寻音效分类表的数据
        $this->db->select('*');
        $this->db->from($this->music_type);
        $res = $this->db->get();
        $datas= $res->result_array();
        //
        $length=count($datas);
        $da1=[];
        if(!empty($datas)){
            foreach($datas as $k=>$v){
                $da1['music_type_name']=$v['test_music_type_name'];
                $da1['music_type_icon']=$v['test_music_type_icon'];
                $da1['no_music_type_icon']=$v['test_no_music_type_icon'];
                $da1['music_type_status']=$v['test_music_type_status'];
                if($k==$length-1){
                    return $this->db->update($this->music_type,$da1,['id'=>$v['id']]);
                }else{
                    $this->db->update($this->music_type,$da1,['id'=>$v['id']]);
                }
            }
        }
    }
}